import VueRouter from 'vue-router'
import Vue from 'vue'
import { getItem } from '@/utils/storage.js'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirecct: '/login'
    },
    {
      path: '/layout',
      component: () => import('../views/Layout/Layout.vue'),
      children: [
        {
          path: '/',
          component: () => import('../views/Layout/components/home.vue')
        },
        {
          path: 'article',
          component: () => import('../views/Layout/components/article.vue')
        },
        {
          path: 'publish',
          component: () => import('../views/Layout/components/publish.vue')
        }
      ]
    },
    {
      path: '/login',
      component: () => import('../views/login.vue')
    }
  ] // 路由规则
})
router.beforeEach((to, from, next) => {
  // 获取token
  const token = getItem('geek-client-pc-store')
  // 不是访问登录，有没有token，跳转登录页面
  if (to.path !== '/login' && !token) return next('/login')
  // 其他情况放行
  next()
})
export default router
